//1.引入包
const express=require('express');
const expressStatic=require('express-static');
const slideRouter=require('./routes/slide');
const userRouter=require('./routes/user');
const childRouter=require('./routes/child');
const sheeRouter=require('./routes/shee');
const heRouter=require('./routes/he');
const mobileRouter=require('./routes/mob');
const viewsRouter=require('./routes/views');
const dbConnect=require('./utils/db/mongodbConnect');
const bodyParer=require('body-parser');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
//2.创建服务
var app=express();

app.listen(8080,function(err){
    if(err){
        console.log('app is error')
    }else {
        console.log('app is running')
    }
});
//开启数据库连接
dbConnect.dbConnect();

//使用bodyParser
app.use(bodyParer.urlencoded({extend:true}));
//启用cookie
var keys=[];
for(var i=0;i<100;i++){
    var str='abcd1333%#%'+Math.random(100000);
    keys.push(str);
}
app.use(cookieParser());
app.use(cookieSession({
    name:'storeOnline',
    keys:keys,
    maxAge:20*60*1000
}));

//路由
app.use('/slide',slideRouter.router);
app.use('/user',userRouter.router);
app.use('/views',viewsRouter.router);
app.use('/child',childRouter.router);
app.use('/she',sheeRouter.router);
app.use('/he',heRouter.router);
app.use('/mob',mobileRouter.router);
//起始页
app.use('/',function(req,res,next){
    var options={
        root:__dirname+'/www/',
        dotfiles:'deny',
        headers:{
            'x-timestamp': Date.now(),
            'x-sent': true
        }
    };
    var url=req.url;
    //console.log(url.lastIndexOf('/')+'----------'+url.length);
    if(url.indexOf('/')==0&&url.length>1){
        next();
    }else {
        res.sendFile('index.html', options, function (err) {
            if (err) {
                console.log('index.html is error!');
            }
        })
    }
});

//静态页面访问

app.use(expressStatic('./www'));
